#include "registerdialog.h"
#include "ui_registerdialog.h"


#include <QPainter>
#include <QPainterPath>
#include <QMessageBox>
#include "./tools/ui/draghandler.h"
#include "./tools/ui/paintshadow.h"
#include "./entities/constants.h"
#include "./tools/logic/clientmessagehandler.h"

RegisterDialog::RegisterDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::RegisterDialog)
{
    ui->setupUi(this);
    this->setModal(true);  // 设置为模态对话框
    m_shadowWidth = 8;
    m_radius = 8;

    // 移除标题栏和边框
    setWindowFlags(Qt::FramelessWindowHint);
    // 设置透明背景，要不然阴影绘制没效果
    setAttribute(Qt::WA_TranslucentBackground);

    DragHandler* dragHandler = new DragHandler(this);
    installEventFilter(dragHandler);

    // 发送数据给Handler打包
    connect(this, &RegisterDialog::sendRegisterInfo, &ClientMessageHandler::getInstance(), &ClientMessageHandler::sendRegisterRequestMessage);
    // 接收Handler解析好的数据
    connect(&ClientMessageHandler::getInstance(), &ClientMessageHandler::getRegisterResponse, this, &RegisterDialog::DealRegisterResponseMessage);


}

RegisterDialog::~RegisterDialog()
{
    delete ui;
}


void RegisterDialog::on_Btn_Submit_clicked()
{
    QString inputEmail = ui->Input_Email->text();
    QString inputNickname = ui->Input_Nickname->text();

    QString inputPassword = ui->Input_Password->text();
    emit sendRegisterInfo(inputEmail, inputNickname, inputPassword);
}

void RegisterDialog::DealRegisterResponseMessage(const QString status, const QString reason, const QString userId)
{
    if (status == JsonValue::STATUS_SUCCESS)
    {
        // 注册成功 跳转登录界面
        QString tip = tr("牢记id：") + userId;
        QMessageBox::information(this, tr("注册成功"), tip);
        accept();
        return;
    }

    Reason failureReason = reason;
    if (failureReason == JsonValue::REASON_FAILURE_UNKNOWN_ERROR)
    {
        QMessageBox::critical(this, tr("注册失败"), tr("未知错误"));
    }
    else if (failureReason == JsonValue::REASON_FAILURE_EMAIL_ALREADY_EXISTS)
    {
        QMessageBox::critical(this, tr("注册失败"), tr("邮箱已存在"));
    }

}

void RegisterDialog::on_Btn_BackToLogin_clicked()
{
    accept();
}

void RegisterDialog::paintEvent(QPaintEvent *event)
{

    // 绘制图片背景
    QPainter painter(this);
    painter.setPen(Qt::NoPen);  // 不显示边框
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);   // 开启平滑渲染更丝滑
    painter.setRenderHint(QPainter::Antialiasing, true);    // 开启抗锯齿更丝滑

    if (!m_backgroundCache.isNull()) {
        // 设置圆角矩形裁剪路径
        QPainterPath path;
        path.addRoundedRect(m_shadowWidth, m_shadowWidth, this->width() - 2 * m_shadowWidth, this->height() - 2 * m_shadowWidth, m_radius, m_radius);
        painter.setClipPath(path);  // 设置剪切区域
        // 将背景图片填充到窗口区域
        painter.drawPixmap(m_shadowWidth, m_shadowWidth, this->width() - 2 * m_shadowWidth, this->height() - 2 * m_shadowWidth, m_backgroundCache);
    }

    // 绘制阴影边框
    QPainter painter2(this);
    // 直接绘制缓存里的阴影边框
    painter2.drawPixmap(0, 0, m_shadowCache);

    // 绘制灰色半透明背景(无需计算和文件操作，没必要消耗内存当缓存)
    QPainter painter3(this);
    int margin = 30;
    // 设置半透明灰色
    QColor transparentGray(255, 255, 255, 100);  // RGB + alpha
    painter3.setBrush(QBrush(transparentGray));
    painter3.setPen(Qt::NoPen);  // 不显示边框
    // 绘制矩形，位置为(50, 50)，宽度为300，高度为200
    painter3.drawRect(m_shadowWidth + margin, m_shadowWidth + margin, this->width() - 2*(m_shadowWidth + margin), this->height() - 2*(m_shadowWidth + margin));
}

void RegisterDialog::resizeEvent(QResizeEvent *event)
{
    // 更新阴影边框的缓存
    m_shadowCache = QPixmap(this->size());  // 创建一个与窗口大小一致的透明图层
    m_shadowCache.fill(Qt::transparent);  // 填充透明色
    PaintShadow sp(&m_shadowCache, m_shadowWidth, m_radius);

    // 更新背景图片的缓存
    m_backgroundCache = QPixmap(":/images/mainBackground.jpg");

    QDialog::resizeEvent(event);

}




